home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / dialip10.zip / GETPOP.CMD < prev    next >
OS/2 REXX Batch file  |  1996-11-18  |  8KB  |  279 lines

  1. /*------------------------------------------------------------------
  2.  * getpop.cmd :
  3.  *------------------------------------------------------------------
  4.  * 08-09-92 rnr.cmd published originally by Patrick J. Mueller
  5.  * 24-01-95 adapted as chkpop.cmd by Christoph Lechleitner
  6.  * 15-06-96 fixed for more RFC-compatibility by C. Lechleitner
  7.  * 15-06-96 adapted to getpop.cmd by Christoph Lechleitner
  8.  * Nov-96 Added code to notify user of new mail.  Stan J. Towianski
  9.  *------------------------------------------------------------------*/
  10.  
  11. trace off
  12.  
  13. parse arg server user password newmailfilemask
  14. say 'entered with newmailfilemask ='newmailfilemask'='
  15.  
  16. if (server = "") | (user='') | (password='') | (newmailfilemask='') then
  17.    do
  18.    say "Expecting a pop server name, a user, a password and a Filemask as parameters."
  19.    exit 0
  20.    end
  21. say ' '
  22.  
  23.  
  24. /*------------------------------------------------------------------
  25.  * initialize socket function package
  26.  *------------------------------------------------------------------*/
  27. parse source os .
  28.  
  29. call RxFuncAdd 'SysTempFileName', 'RexxUtil', 'SysTempFileName'
  30. if (os = "OS/2") then
  31.    do
  32.    if RxFuncQuery("SockLoadFuncs") then
  33.       do
  34.       rc = RxFuncAdd("SockLoadFuncs","RxSock","SockLoadFuncs")
  35.       rc = SockLoadFuncs()
  36.       rc = RxFuncAdd("SysTempFileName","RexxUtil","SysTempFileName")
  37.       end
  38.    end
  39.  
  40. if (os = "AIX/6000") then
  41.    do
  42.    rc = SysAddFuncPkg("rxsock.dll")
  43.    end
  44.  
  45. /*------------------------------------------------------------------
  46.  * get address of server
  47.  *------------------------------------------------------------------*/
  48. rc = SockGetHostByName(server,"host.!")
  49. if (rc = 0) then
  50.    do
  51.    say "Unable to resolve server name" server
  52.    exit 0
  53.    end
  54.  
  55. server = host.!addr
  56.  
  57. /*------------------------------------------------------------------
  58.  * open socket
  59.  *------------------------------------------------------------------*/
  60. sock = SockSocket("AF_INET","SOCK_STREAM","IPPROTO_TCP")
  61. if (sock = -1) then
  62.    do
  63.    say "Error opening socket:" errno
  64.    exit 0
  65.    end
  66.  
  67. /*------------------------------------------------------------------
  68.  * connect socket
  69.  *------------------------------------------------------------------*/
  70. server.!family = "AF_INET"
  71. server.!port   = 110
  72. server.!addr   = server
  73.  
  74. rc = SockConnect(sock,"server.!")
  75. if (rc = -1) then
  76.    Error(sock,rc,"Error connecting to popserver :" errno)
  77.  
  78.    trc = GetResponse(sock)
  79.  
  80.    trc = SendMessage(sock,'USER 'user)
  81.    trc = GetResponse(sock)
  82.    parse var line.1 status rest
  83.    if status <> '+OK' then
  84.      do
  85.        say ' Error: User' user 'unknown on' server '.'
  86.        qrc = SendMessage(sock,'QUIT')
  87.        qrc = SockSoclose(sock)
  88.        exit 0
  89.      end
  90.  
  91.    trc = SendMessage(sock,'PASS 'password)
  92.    trc = GetResponse(sock)
  93.    parse var line.1 status rest
  94.    if status <> '+OK' then
  95.      do
  96.        say ' Error: Password wrong for' user ' on 'server'.'
  97.        qrc = SendMessage(sock,'QUIT')
  98.        qrc = SockSoclose(sock)
  99.        exit 0
  100.      end
  101.    else
  102.      do 
  103.        trc = SendMessage(sock,'LIST')
  104.        trc = GetResponse(sock)
  105.        messages = 0
  106.        do 
  107.          msginfo = GetResponseLine(sock)
  108.          do while msginfo <> '.'
  109.            messages = messages + 1
  110.            msginfo = GetResponseLine(sock)
  111.          end
  112.          if messages = 0 
  113.          then say ' There is no message waiting for you.'
  114.          else
  115.            do 
  116.              say ' There are' messages 'messages waiting for you.'
  117.              'start /min notify' messages
  118.              trc = SendMessage(sock,'LIST')
  119.              trc = GetResponse(sock)
  120.              do 
  121.                msginfo = GetResponseLine(sock)
  122.                do while msginfo <> '.'
  123.                  parse var msginfo number size
  124.                  say ' Message' number 'has' size 'bytes.'
  125.                  msginfo = GetResponseLine(sock)
  126.                end
  127.              end /* do */
  128.              /* Get Mails */
  129.              do i = 1 to messages
  130.                say ' Getting Message' i
  131.                trc = SendMessage(sock,'RETR ' i)
  132.                trc = GetResponse(sock)
  133.                parse var line.1 status rest
  134.                newMailFile = SysTempFileName(newmailfilemask)
  135.                if newmailfile = '' then newmailfile = newmailfilemask
  136.                say 'newMailFile ='newMailFile'='    'newmailfilemask ='newmailfilemask'='
  137.                oneline = GetResponseLine(sock)
  138.                do while oneline <> '.'
  139.                  call lineout newMailFile, oneline
  140.                  oneline = GetResponseLine(sock)
  141.                end
  142.                call lineout newMailFile
  143.                trc = SendMessage(sock,'DELE ' i)
  144.                trc = GetResponse(sock)
  145.                parse var line.1 status rest
  146.                say ' Got and deleted Message' i
  147.              end  
  148.        end
  149.  
  150.    trc = SendMessage(sock,'QUIT')
  151.    trc = GetResponse(sock)
  152.  
  153. /*------------------------------------------------------------------
  154.  * quittin' time!
  155.  *------------------------------------------------------------------*/
  156. /* rc = SendMessage(sock,"quit") */
  157. rc = SockSoclose(sock)
  158. exit messages
  159.  
  160.  
  161. /*------------------------------------------------------------------
  162.  * help
  163.  *------------------------------------------------------------------*/
  164. Help: procedure
  165.    say "commands:"
  166.    say
  167.    say "quit    - to quit"
  168.    say "group   - to change to a particular group"
  169.    say "article - to see an article"
  170.    say
  171.    return ""
  172.  
  173. /*------------------------------------------------------------------
  174.  * get a response from the server
  175.  *------------------------------------------------------------------*/
  176. GetResponse:     procedure expose !. line.
  177.    sock = arg(1)
  178.  
  179.    moreids = "100 215 220 221 222 223 230 231"
  180.  
  181.    line.0 = 1
  182.    line.1 = GetResponseLine(sock)
  183.  
  184.    parse var line.1 rid .
  185.  
  186.    if (wordpos(rid,moreids) = 0) then
  187.       return ""
  188.  
  189.    say ' getting further lines '
  190.  
  191.    do forever
  192.       o = line.0 + 1
  193.  
  194.       line.o = GetResponseLine(sock)
  195.  
  196.       if (line.o = ".") then
  197.          return ""
  198.  
  199.       line.0 = o
  200.    end
  201.  
  202.    return ""
  203.  
  204. /*------------------------------------------------------------------
  205.  * get a line from the server
  206.  *------------------------------------------------------------------*/
  207. GetResponseLine: procedure expose !.
  208.    sock = arg(1)
  209.  
  210.    crlf = d2c(13) || d2c(10)
  211.  
  212.    if (symbol('!.buff') = "LIT") then
  213.       !.buff = ""
  214.  
  215.    do while (pos(crlf,!.buff) = 0)
  216.       rc = SockRecv(sock,"data",8000)
  217.       !.buff = !.buff || data
  218.    end
  219.  
  220.    /* say ' got data "' data '"' */
  221.    /* say ' buff = "' !.buff '"' */
  222.  
  223.  
  224.    p = pos(crlf,!.buff)
  225.  
  226.    line = substr(!.buff,1,p-1)
  227.    !.buff = substr(!.buff,p+2)
  228.  
  229.    return line
  230.  
  231. /*------------------------------------------------------------------
  232.  * send a string to the server
  233.  *------------------------------------------------------------------*/
  234. SendMessage:     procedure expose !.
  235.    sock = arg(1)
  236.    data = arg(2) || d2c(13) || d2c(10)
  237.  
  238.    /* say 'Sending "'data'" to server.' */
  239.    len = length(data)
  240.    do while (len > 0)
  241.  
  242.       len = SockSend(sock,data);
  243.       /* say 'Returncode: ' len   */
  244.       /* say 'Errorcode:  ' errno */
  245.       /*
  246.       if (errno <> 0) then
  247.          Error(-1,rc,"Error sending data to server.")
  248.       */
  249.       if (len <= 0) then
  250.          Error(sock,100,"Server closed the connection.")
  251.       
  252.       data = substr(data,len+1)
  253.       len  = length(data)
  254.    end
  255.  
  256.    return i
  257.  
  258. /*------------------------------------------------------------------
  259.  * halting ...
  260.  *------------------------------------------------------------------*/
  261. Halting:
  262.    Error(sock,1,"error on line" sigl)
  263.  
  264. /*------------------------------------------------------------------
  265.  * exit with a message and return code
  266.  *------------------------------------------------------------------*/
  267. Error: procedure
  268.    sock = arg(1)
  269.    retc = arg(2)
  270.    msg  = arg(3)
  271.  
  272.    if (sock <> -1) then
  273.       rc = SockSoClose(sock)
  274.  
  275.    say msg
  276.  
  277.    exit retc
  278.  
  279.